<template>
	<div class="system-menu-container">
		<el-card shadow="hover">
			<div class="system-menu-search mb15">
        <el-form :inline="true">
          <el-form-item>
            <el-button size="default" type="primary" class="ml10" @click="handleQuery">
              <el-icon>
                <ele-RefreshRight />
              </el-icon>
              刷新
            </el-button>
            <el-button size="default" type="success" class="ml10" @click="onOpenRegMenu(null)" v-auth="'api/v1/system/menu/add'">
              <el-icon><ele-RefreshRight /></el-icon>
              更新菜单
            </el-button>
          </el-form-item>
        </el-form>
			</div>
			<el-table :data="menuTableData" style="width: 100%" row-key="path" :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
        <el-table-column label="菜单名称" show-overflow-tooltip>
					<template #default="scope">
						<SvgIcon :name="scope.row.icon" />
						<span class="ml10">{{ scope.row.title }}</span>
					</template>
				</el-table-column>
				<el-table-column prop="path" label="菜单路由" show-overflow-tooltip></el-table-column>
        <el-table-column prop="name" label="菜单标识" show-overflow-tooltip></el-table-column>
        <el-table-column prop="is_auth" label="菜单类型" show-overflow-tooltip>
          <template #default="scope">
            <el-tag type="success" v-if="scope.row.status===1">公共</el-tag>
            <el-tag type="info" v-else>权限</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" show-overflow-tooltip>
          <template #default="scope">
            <el-tag type="success" v-if="scope.row.status===1">启用</el-tag>
            <el-tag type="info" v-else>禁用</el-tag>
          </template>
        </el-table-column>
      </el-table>
		</el-card>
		<EditMenu ref="editMenuRef" @menuList="menuList" :visibleOptions="sys_show_hide" :acType="acType"/>
	</div>
</template>

<script lang="ts">
import {ref, toRefs, reactive, onBeforeMount, defineComponent, getCurrentInstance, unref} from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import EditMenu from '/@/views/system/menu/component/editMenu.vue';
import {delMenu, getMenuList} from "/@/api/system/menu";
import {dynamicRoutes} from "/@/router/route";
import {AdminRegMenu} from "/@/api/menu";
export default defineComponent({
	name: 'apiV1SystemAuthMenuList',
	components: { EditMenu },
	setup() {
		const editMenuRef = ref();
		const state = reactive({
      queryParams:{
        title:"",
        component:""
      },
      menuTableData:[],
    });
    const {proxy} = getCurrentInstance() as any;
    const {sys_show_hide} = proxy.useDict('sys_show_hide')
    const acType = ref('add')
		// 打开新增菜单弹窗
		const onOpenRegMenu = (row:any) => {
      var data = {'data':dynamicRoutes}
      let jsonString = JSON.stringify(data);
      console.log(jsonString);
      AdminRegMenu(JSON.stringify(data)).then(res=>{
        if (res.code == 200){
          ElMessage.success('同步成功');
          menuList()
        }else {
          ElMessage.success('同步失败，请稍后重试');
        }
      })
		};
    onBeforeMount(()=>{
      menuList()
    });
    const handleQuery=() => {
      menuList();
    };
    const menuList = ()=>{
      getMenuList(state.queryParams).then(res=>{
        if (res.code == 200){
          state.menuTableData=res.data;
        }

        // state.menuTableData = proxy.handleTree(res.data.rules??[], "id","pid");
      })
    };
		return {
			editMenuRef,
      onOpenRegMenu,
      menuList,
      handleQuery,
			...toRefs(state),
      sys_show_hide,
      acType
		};
	},
});
</script>
